package hesso.smartdraw.ch;

import hesso.smartdraw.ch.db.DBHelper;
import hesso.smartdraw.ch.multitouch.MultiTouch;
import hesso.smartdraw.ch.multitouch.MultiTouchController;
import hesso.smartdraw.ch.multitouch.MultiTouchException;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;


import android.content.Intent;
import android.view.Display;
import android.widget.Toast;

public class StartScreen extends Base{
	  // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH  = 2000;
    private static final int CAMERA_HEIGHT = 500;

    // ===========================================================
    // Fields
    // ===========================================================

    private Camera mCamera;
    private Texture mTexture;
    private TextureRegion mFaceTextureRegion;
    private DBHelper dbhelper;
    
    private Display display;

	private Texture mFontTexture;
	
	private Font mFont;

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public Engine onLoadEngine() {
    	dbhelper = DBHelper.getInstance(this);
    	
    	dbhelper.initDBConn();

    	display = getWindowManager().getDefaultDisplay(); 
    	
            this.mCamera = new Camera(0, 0, display.getWidth(), display.getHeight());
            final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(display.getWidth(), display.getHeight()), this.mCamera));

            try {
                    if(MultiTouch.isSupported(this)) {
                            engine.setTouchController(new MultiTouchController());
                            if(MultiTouch.isSupportedDistinct(this)) {
                                    Toast.makeText(this, "MultiTouch detected", Toast.LENGTH_LONG).show();
                            } else {
                                    Toast.makeText(this, "MultiTouch detected --> (Your device might have problems to distinguish between separate fingers.)", Toast.LENGTH_LONG).show();
                            }
                    } else {
                            Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)", Toast.LENGTH_LONG).show();
                    }
            } catch (final MultiTouchException e) {
                    Toast.makeText(this, "Sorry your Android Version does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)", Toast.LENGTH_LONG).show();
            }

            return engine;
    }

    @Override
    public void onLoadResources() {
    	this.mTexture = new Texture(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "logo/fireflyLogo.png", 0, 0);

        this.mEngine.getTextureManager().loadTexture(this.mTexture);
    }

    @Override
    public Scene onLoadScene() {
            this.mEngine.registerUpdateHandler(new FPSLogger());

            final Scene scene = new Scene(1);
            scene.setOnAreaTouchTraversalFrontToBack();

            scene.setBackground(new ColorBackground(0.4f, 0.4f, 0.4f));

            scene.setTouchAreaBindingEnabled(true);
            
            final Sprite logo = new Sprite(30, 80, this.mFaceTextureRegion);
            
            scene.attachChild(logo);
            
        
        	
        	scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
                @Override
                public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
                        switch(pSceneTouchEvent.getAction()) {
                                case TouchEvent.ACTION_DOWN:
                                	Intent intent = new Intent(StartScreen.this,LoginRegisterScreen.class);
                                	startActivity(intent);
                                        break;
                                case TouchEvent.ACTION_UP:
                                    
                                        break;
                        }
                        return true;
                }
        });

            return scene;
    }

    @Override
    public void onLoadComplete() {
    	
    	

    }
    

    // ===========================================================
    // Methods
    // ===========================================================

   

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
